/**
 * Created by XianTao on 2016/11/22
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.biz.wechat.lechuan.mobile.api.handler;

import org.lego.wechat.base.msg.in.InMsg;
import org.lego.wechat.base.msg.in.event.InMenuEvent;
import org.lego.wechat.base.msg.out.OutMsg;
import org.lego.wechat.base.msg.out.OutTextMsg;
import org.lego.wechat.portal.service.AbstractWechatHandler;
import org.lego.wechat.portal.service.WechatHandler;
import org.springframework.stereotype.Service;

/**
 * 菜单事件处理器
 */
@Service
public class MenuHandler extends AbstractWechatHandler {
    public boolean isMatch(InMsg inMsg) {
        return inMsg instanceof InMenuEvent;
    }

    public OutMsg process(InMsg inMsg) {
        if (inMsg instanceof InMenuEvent) {
            InMenuEvent inMenuEvent = (InMenuEvent) inMsg;
            if (InMenuEvent.EVENT_INMENU_CLICK.equalsIgnoreCase(inMenuEvent.getEvent())) {
                if ("我的礼品".equals(inMenuEvent.getEventKey())) {
                    OutTextMsg outTextMsg = new OutTextMsg(inMsg);
                    outTextMsg.setContent("<a href='http://lechuan.tunnel.qydev.com/mobile/showPrize.html?openId=" + inMsg.getFromUserName() + "'>点击查看您的礼品</a>");
                    return outTextMsg;
                }
            } else if (InMenuEvent.EVENT_INMENU_VIEW.equalsIgnoreCase(inMenuEvent.getEvent())) {
                throw new RuntimeException("事件类型" + inMenuEvent.getEvent() + "在控制之中，暂时无需进行任何处理");
            }
        }
        return null;
    }

    public WechatHandler getMyself() {
        return this;
    }
}
